package com.code.day04_utiles;

import com.code.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Demo03DBUtils {

    //无参QueryRunner
    @Test
    public void insert() throws SQLException {
       QueryRunner runner = new QueryRunner();
       Connection conn = DruidUtils.getConnection();
       String sql = "insert into `category`(cname) values(?)";

       runner.update(conn,sql,"大炮");
       DruidUtils.closeConnection(conn,null,null);

    }

    //有参QueryRunner,可以不用手动关闭资源,需要新增一个getDataSource方法
    @Test
    public void delete() throws SQLException {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());

        String sql = "delete from `category` where cname = ?";

        runner.update(sql,"大炮");


    }

}
